package it.sd.wdc.spider.repository.template;

import java.util.Iterator;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;



public class SpiderNodeTemplate extends AbstractNode {
	
	private String regexp;
	private String name;
	private boolean ignoreContent = false;
	private boolean removeTag = false;
	private boolean trimSpace = false;
	private int item;
	
	public SpiderNodeTemplate(String tag) {
		this.name = tag;
	}

	public String getRegexp() {
		return regexp;
	}

	public void setRegexp(String regexp) {
		this.regexp = regexp;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getItem() {
		return item;
	}

	public void setItem(int item) {
		this.item = item;
	}
	public void setItem(String item) {
		if (item!=null) {
			if (item.trim().length()>0) {
				this.item = Integer.parseInt(item);
			}
		}
	}

	public boolean isIgnoreContent() {
		return ignoreContent;
	}

	public void setIgnoreContent(boolean ignoreContent) {
		this.ignoreContent = ignoreContent;
	}	

	public String toString() {
		StringBuffer b = new StringBuffer();

		b.append(getUniquePath()).append("-").append(name);
//		for (Iterator<? extends AbstractNode> it=iterateFigli(); it.hasNext(); ) {
//			AbstractNode figlio = it.next();
//			b.append("\n").append(figlio);
//		}
		
		return b.toString();
	}

	public Document toXML() throws Exception {
		Document doc = null;
		
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setNamespaceAware(true); 
		doc = domFactory.newDocumentBuilder().newDocument();
		
		buildXml(doc, null);
		
		return doc;
	}
	
	private void buildXml(Document doc, Element parent) {
		
		Element e = doc.createElement(getName());
		if (parent==null) {
			doc.appendChild(e);
		} else {
			parent.appendChild(e);
		}
		
		if (regexp!=null) {
			e.setAttribute("regexp", regexp);
		}
		if (ignoreContent) {
			e.setAttribute("ignoreContent", "true");
		}
		if (trimSpace) {
			e.setAttribute("trimSpace", "true");
		}
		if (removeTag) {
			e.setAttribute("removeTag", "true");
		}
		if (item>0) {
			e.setAttribute("item", String.valueOf(item));
		}
		
		Iterator<? extends AbstractNode> it = iterateFigli();
		while (it.hasNext()) {
			SpiderNodeTemplate f = (SpiderNodeTemplate)it.next();
			f.buildXml(doc, e);
		}
	}

	/**
	 * @return the removeTag
	 */
	public boolean isRemoveTag() {
		return removeTag;
	}

	/**
	 * @param removeTag the removeTag to set
	 */
	public void setRemoveTag(boolean removeTag) {
		this.removeTag = removeTag;
	}

	/**
	 * @return the trimSpace
	 */
	public boolean isTrimSpace() {
		return trimSpace;
	}

	/**
	 * @param trimSpace the trimSpace to set
	 */
	public void setTrimSpace(boolean trimSpace) {
		this.trimSpace = trimSpace;
	}
	
	
}
